﻿package com.projects.xenia.add_activities;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import com.projects.xenia.R;
import com.projects.xenia.documents_classes.Documents_Order;
import com.projects.xenia.global_modules.MainGlobalModule;
import com.projects.xenia.ref_classes.OffersRefItem;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Activity_SalesHistory extends Activity {
	
	private ArrayList <SalesHistoryItem> SalesHistoryItems;
	
	private ImageView saleshistory_BackWard;
	
	private Context Ctx;
	
	private ListView SalesHistoryListView;
	
	private TextView SalesHistoryTextView;
	
	private SalesHistoryCustomListAdapter SalesHistoryListViewAdapter;
	
	private String CustomersId;
	private String CustomersDescr;
	
	private String GoodsId;
	private String GoodsDescr;
	
	private class SalesHistoryItem {
		
		public int days;
		public String date;
		public Double rest;
		public Double order;
		public Double restsum;
		public Double ordersum;
		
	}
	
	private void GetSalesHistoryList () {
		
		SalesHistoryItems = new ArrayList <SalesHistoryItem> (); 
		
		try {
			Cursor HistoryCursor = MainGlobalModule.DBE.rawQuery("select date (h.date_create), sum (t.restvalue), sum (t.ordervalue*t.factor), sum (t.order_sum) , sum (t.rest_sum) from xdoc_orders_h h inner join xdoc_orders_t t on h.id = t.hdoc_id where h.customers_id = ? and t.goods_id = ? and date (h.date_create) < date('now') group by date (h.date_create) order by h.date_create desc", new String [] {CustomersId,GoodsId});
			if (HistoryCursor.getCount()>0) {
				HistoryCursor.moveToFirst();
				while (!HistoryCursor.isAfterLast()) {				
					SalesHistoryItem hi = new SalesHistoryItem ();
					hi.days = 0;
					hi.date = HistoryCursor.getString(0); 
					hi.rest = HistoryCursor.getDouble(1);
					hi.order = HistoryCursor.getDouble(2);					
					hi.ordersum = HistoryCursor.getDouble(3);
					hi.restsum = HistoryCursor.getDouble(4);
					SalesHistoryItems.add(hi);
					HistoryCursor.moveToNext();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);            
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
	
		setContentView(R.layout.saleshistory_form);
		
		saleshistory_BackWard = (ImageView)findViewById(R.id.saleshistory_form_BackWard);
		saleshistory_BackWard.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				finish ();
			}
		});
		
		Ctx = this;
		
		Bundle DocExtras;
		DocExtras = getIntent().getExtras();
		
		CustomersId = DocExtras.getString("CustomersId");
		CustomersDescr = DocExtras.getString("CustomersDescr");
		
		GoodsId = DocExtras.getString("GoodsId");
		GoodsDescr = DocExtras.getString("GoodsDescr");
		
		GetSalesHistoryList ();
		
		SalesHistoryListView = (ListView)findViewById(R.id.saleshistory_form_ListView);
		SalesHistoryListViewAdapter = new SalesHistoryCustomListAdapter (this);
		SalesHistoryListView.setAdapter(SalesHistoryListViewAdapter);
		
		SalesHistoryTextView = (TextView)findViewById(R.id.saleshistory_form_TextView);
		SalesHistoryTextView.setText(String.format("История продаж товара %n %s ", GoodsDescr));
		
	}
	
	private class SalesHistoryCustomListAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		public Context context;
		
		public SalesHistoryCustomListAdapter(Context context) {			
			
			mInflater = LayoutInflater.from(context);
			this.context = context;	
		
		}

		@Override
		public int getCount() {	
			return SalesHistoryItems.size();
		}

		@Override
		public SalesHistoryItem getItem(int index) {			
			return SalesHistoryItems.get(index);
		}

		@Override
		public long getItemId(int index) {
			return index;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
		
			ViewHolder holder;
			
			if (convertView == null) {			
				convertView = mInflater.inflate(R.layout.saleshistory_form_list, null);
			}
			
			holder = new ViewHolder();
			
			SalesHistoryItem it  = getItem(position);
						
			
			holder.days = (TextView)convertView.findViewById(R.id.saleshistory_days);
			holder.days.setText(String.format("%s", "-"));
			
			holder.date = (TextView)convertView.findViewById(R.id.saleshistory_date);
			holder.date.setText(String.format("%s", it.date));
			
			holder.order = (TextView)convertView.findViewById(R.id.saleshistory_order);
			holder.order.setText(String.format("%.1f", it.order));
			
			holder.rest = (TextView)convertView.findViewById(R.id.saleshistory_rest);
			holder.rest.setText(String.format("%.1f", it.rest));
			
			holder.ordersum = (TextView)convertView.findViewById(R.id.saleshistory_ordersum);
			holder.ordersum.setText(String.format("%.2f", it.ordersum));
			
			holder.restsum = (TextView)convertView.findViewById(R.id.saleshistory_restsum);
			holder.restsum.setText(String.format("%.2f", it.restsum));
			
			
			return convertView;			
		}
		
		class ViewHolder {
			
			TextView days;
			TextView date;
			TextView order;
			TextView rest;
			TextView ordersum;
			TextView restsum;
				
		}

	}
	
}
